//
//  RecommendVC.m
//  YuDou
//
//  Created by yingqi on 2018/6/22.
//  Copyright © 2018年 yingqi. All rights reserved.
//

#import "RecommendVC.h"
#import "RecommendVM.h"
#import "CycleModel.h"
#import "RecommendCycleView.h"
#import "RecommendGameView.h"

#define kCycleViewH  kScreenWidth * 3 / 8
#define kGameViewH 90

@interface RecommendVC ()<UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) RecommendCycleView *cycleView;
@property (nonatomic, strong) RecommendGameView *gameView;
@property (nonatomic , strong) AnchorGroup *hottestGroup;
@property (nonatomic , strong) AnchorGroup *prettyGroup;
@end

@implementation RecommendVC

- (AnchorGroup *)hottestGroup
{
    if (!_hottestGroup) {
        _hottestGroup = [[AnchorGroup alloc]init];
        _hottestGroup.tag_name = @"最热";
        _hottestGroup.icon_name = @"home_header_hot";
    }
    return _hottestGroup;
}

- (AnchorGroup *)prettyGroup
{
    if (!_prettyGroup) {
        _prettyGroup = [[AnchorGroup alloc]init];
        _prettyGroup.tag_name = @"颜值";
        _prettyGroup.icon_name = @"columnYanzhiIcon";
    }
    return _prettyGroup;
}

- (RecommendCycleView *)cycleView
{
    if (!_cycleView) {
        _cycleView = [RecommendCycleView recommendCycleView];
        _cycleView.frame = CGRectMake(0, -(kCycleViewH+kGameViewH), kScreenWidth, kCycleViewH);
    }
    return _cycleView;
}

- (RecommendGameView *)gameView
{
    if (!_gameView) {
        _gameView = [RecommendGameView recommendGameView];
        _gameView.frame = CGRectMake(0,  -kGameViewH,  kScreenWidth, kGameViewH);
    }
    return _gameView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)loadData
{
    __weak typeof(self) weak_self = self;
    
    dispatch_group_t group = dispatch_group_create();
   
    dispatch_group_enter(group);
    [RecommendVM requestCycleData:^(id returnValue) {
        [weak_self handelCycleDataData:returnValue];
        dispatch_group_leave(group);
        
    }];
    
    dispatch_group_enter(group);
    [RecommendVM getAnchorGroups:^(id anchorGroups) {
        [weak_self handelAnchorGroupsData:anchorGroups];
        dispatch_group_leave(group);
        
    }];
    
    dispatch_group_enter(group);
    [RecommendVM getHottestGroup:^(NSArray *hottestGroups) {
        weak_self.hottestGroup.anchors = hottestGroups;
        dispatch_group_leave(group);
        
    }];
    
    dispatch_group_enter(group);
    [RecommendVM getPrettyGroup:^(NSArray *prettyGroups) {
        weak_self.prettyGroup.anchors = prettyGroups;
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [weak_self.anchorGroups insertObject:self.prettyGroup atIndex:0];
        [weak_self.anchorGroups insertObject:self.hottestGroup atIndex:0];
        [weak_self.collectionView reloadData];
        [weak_self loadDataFinished];
    });
}

- (void)setupUI
{
    [super setupUI];
    [self.collectionView addSubview:self.cycleView];
    [self.collectionView addSubview:self.gameView];
    self.collectionView.contentInset = UIEdgeInsetsMake(kCycleViewH+kGameViewH,  0,  0, 0);
    
}

- (void)handelAnchorGroupsData:(NSArray *)returnValue
{
    self.gameView.groups = returnValue;
    [self.anchorGroups addObjectsFromArray:returnValue];
}

- (void)handelCycleDataData:(NSArray *)returnValue
{
    self.cycleView.cycleModels = returnValue;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1){
        return CGSizeMake(kNormalItemW, kPrettyItemH);
    }else{
        return CGSizeMake(kNormalItemW, kNormalItemH);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1){
        CollectionPrettyCell *cell = (CollectionPrettyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:PrettyCellID forIndexPath:indexPath];
        cell.anchor = self.anchorGroups[indexPath.section].anchors[indexPath.item];
        return cell;
    }else{
        return [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
    }
}

@end
